from django.db import models
from django.utils import timezone

class DataSource(models.Model):
    ENGINE_CHOICES = [
        ('mysql', 'MySQL'),
        ('postgresql', 'PostgreSQL'),
        ('oracle', 'Oracle'),
        ('sqlite', 'SQLite'),
    ]
    name = models.CharField(max_length=100, unique=True, verbose_name='数据源名称')
    description = models.TextField(blank=True, null=True, verbose_name='描述')
    engine = models.CharField(max_length=20, choices=ENGINE_CHOICES, verbose_name='数据库类型')
    host = models.CharField(max_length=255, verbose_name='主机地址')
    port = models.IntegerField(verbose_name='端口')
    database = models.CharField(max_length=100, verbose_name='数据库名')
    user = models.CharField(max_length=100, verbose_name='用户名')
    password = models.CharField(max_length=100, blank=True, null=True, verbose_name='密码')
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    updated_at = models.DateTimeField(auto_now=True, verbose_name='更新时间')
    table_count = models.IntegerField(default=0, verbose_name='表数量')
    connection_status = models.BooleanField(default=False, verbose_name='连接状态')

    class Meta:
        verbose_name = '数据源'
        verbose_name_plural = '数据源'
        ordering = ['-created_at']

    def __str__(self):
        return self.name


class SQLConfig(models.Model):
    CONFIG_TYPE_CHOICES = [
        ('custom', '自定义SQL'),
        ('table_fields', '表字段选择'),
    ]
    name = models.CharField(max_length=100, unique=True, verbose_name='SQL配置名称')
    description = models.TextField(blank=True, null=True, verbose_name='描述')
    config_type = models.CharField(max_length=20, choices=CONFIG_TYPE_CHOICES, default='custom', verbose_name='配置类型')
    sql = models.TextField(verbose_name='SQL语句')
    params = models.TextField(blank=True, null=True, verbose_name='SQL参数')
    datasource = models.ForeignKey(DataSource, on_delete=models.CASCADE, verbose_name='关联数据源')
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    updated_at = models.DateTimeField(auto_now=True, verbose_name='更新时间')

    class Meta:
        verbose_name = 'SQL配置'
        verbose_name_plural = 'SQL配置'
        ordering = ['-created_at']

    def __str__(self):
        return self.name

class DataSourceConfig(models.Model):
    datasource = models.OneToOneField(DataSource, on_delete=models.CASCADE, related_name='config', verbose_name='数据源')
    selected_tables = models.JSONField(default=dict, verbose_name='选择的表和字段')
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    updated_at = models.DateTimeField(auto_now=True, verbose_name='更新时间')

    class Meta:
        verbose_name = '数据源配置'
        verbose_name_plural = '数据源配置'

    def __str__(self):
        return f'{self.datasource.name}的配置'


class Agent(models.Model):
    EXECUTION_TYPE_CHOICES = [
        ('interval', '间隔执行'),
        ('cron', 'Cron表达式'),
        ('manual', '手动执行'),
        ('passive', '被动执行'),
    ]
    
    name = models.CharField(max_length=100, unique=True, verbose_name='智能体名称')
    description = models.TextField(blank=True, null=True, verbose_name='描述')
    datasource = models.ForeignKey(DataSource, on_delete=models.CASCADE, verbose_name='关联数据源')
    execution_type = models.CharField(max_length=10, choices=EXECUTION_TYPE_CHOICES, verbose_name='执行方式')
    execution_interval = models.IntegerField(blank=True, null=True, verbose_name='执行间隔(分钟)')
    execution_start_time = models.DateTimeField(blank=True, null=True, verbose_name='间隔执行开始时间')
    cron_expression = models.CharField(max_length=100, blank=True, null=True, verbose_name='Cron表达式')
    target_agent = models.CharField(max_length=100, blank=True, null=True, verbose_name='目标智能体')
    selected_tables = models.JSONField(default=dict, verbose_name='选择的表和字段')
    config_data = models.JSONField(default=dict, verbose_name='配置数据')
    is_active = models.BooleanField(default=True, verbose_name='是否激活')
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    updated_at = models.DateTimeField(auto_now=True, verbose_name='更新时间')
    last_run_time = models.DateTimeField(blank=True, null=True, verbose_name='最后运行时间')
    run_count = models.IntegerField(default=0, verbose_name='运行次数')

    class Meta:
        verbose_name = '智能体'
        verbose_name_plural = '智能体'
        ordering = ['-created_at']

    def __str__(self):
        return self.name